/**
 * Created by AllanXu on 01/03/2016.
 */
define(function (require) {
	var Vue = require('vue'),
		Config = require('config'),
		_ = require('ajax'),
		utilities = require('utilities');

    var id = utilities.getParameterByName('id');

	var app = new Vue({
		el: '#uc_activeRank',
		data: {
			renderObj: {},
			picServer: Config.picServer,
            mainInfo: '',
            pageCount: '',
            pageno: 1,
            applied: [],
			info1:'',
            info2:'',
            info3:'',
			member: {}
		},
		methods: {
			init: function () {
				utilities.refreshUserInfo(function (data) {
					app.renderObj = data;
					app.member = JSON.parse(decodeURIComponent(atob(sessionStorage.userInfo))).member;
				});
                _(Config.SERVICE.ACTIVITY.BLINDPART+'?id='+id, {}, function(data) {
                    app.mainInfo = data.obj;
                    _(Config.SERVICE.ACTIVITY.APPLIEDS+'?blind.id='+app.mainInfo.blind.id+'&type=1&pageno='+app.pageno+'&pagesize=10&id='+sessionStorage.id, {}, function(data) {
                        if (app.pageno == 1) {
                            app.applied = data.pageitems;
                            app.pageCount = data.pagecount;

                            app.info1=app.applied[0];
                            app.info2=app.applied[1];
                            app.info3=app.applied[2];
                        } else {
                            app.applied = app.applied.concat(data.pageitems);
                        }
                    });
                });
			},
			go: function (url) {
				location.href = url;
			},
            checkin: function () {
                var geolocation = new BMap.Geolocation();
                geolocation.getCurrentPosition(app.mapIt);
                //app.mapIt();
            },
            mapIt:function(r){
                var lon = r.point.lng;
                var lat = r.point.lat;
                //var lon="120.091637";
                //var lat="30.326828";

                var point_1 = new BMap.Point(""+lon+"",""+lat+"");
                var maps=app.mainInfo.blind.map.split(',')
                var point_2 = new BMap.Point(""+maps[0]+"",""+maps[1]+"");
                var num=BMapLib.GeoUtils.getDistance(point_1, point_2);
                if(num>1000){
                    alert("您距离活动目的地太远了不能签到！");
                    return;
                }
                _(Config.SERVICE.ACTIVITY.BLINDSAVE, {
                    id: id
                }, function (data) {
                    if (data.code == 200 && data.type === 'SUCCESS') {
                        alert('签到成功！');
                        utilities.refreshUserInfo(function (data) {
                            location.reload();
                        });
                    }
                });
            },
            sh: function (partid) {
                if(confirm("每人只有一次机会，您是否已经考虑好？")){
                    _(Config.SERVICE.ACTIVITY.VOTESAVE+'?id='+partid+'&part.member.id='+app.member.id, {}, function (data) {
                        if (data.code == 200 && data.type === 'SUCCESS') {
                            alert('献花成功！');
                            utilities.refreshUserInfo(function (data) {
                                location.reload();
                            });
                        }
                    });
                }
            },
            sx: function () {
                _(Config.SERVICE.ACTIVITY.BLINDPART+'?id='+id, {}, function(data) {
                    app.mainInfo = data.obj;
                    _(Config.SERVICE.ACTIVITY.APPLIEDS+'?blind.id='+app.mainInfo.blind.id+'&type=1&pageno='+app.pageno+'&pagesize=10&id='+app.member.id, {}, function(data) {
                        app.pageno == 1
                        app.applied = data.pageitems;
                        app.pageCount = data.pagecount;
                        app.info1=app.applied[0];
                        app.info2=app.applied[1];
                        app.info3=app.applied[2];
                    });
                });
            }
		}
	});
	app.init();

    window.onscroll = function(){
        var scrolltop = document.body.scrollTop | document.documentElement.scrollTop;
        var clientHeight = document.documentElement.clientHeight;
        if(scrolltop + clientHeight  == document.body.clientHeight){
            if (app.pageno <= app.pageCount){
                app.pageno++;
                app.init();
            }else{
                app.bottomTxt = '到底儿了~';
            }
        }
    }
});
